<?php
namespace Home\Model;
use Common\Model\BaseModel;

class EquipmentModel extends BaseModel {

    public $tableName = 'equipment';

    /**
     * @param $post
     * @return array
     * 更新或修改合同信息
     */
    public function saveEquipment($data){

        $ret = array('status'=>true,'msg'=>'');
        $post = $this->returnData($data);

        if(!$this->create($post)){

            $ret['status'] = false;
            $ret['msg'] = $this->getError();

        }else{

            
            $res = (null !== $post['equipment_id']) ? $this->save($post) : $this->add($post);
            
            if(false !== $res){
                $ret['msg'] = '操作成功';
            }else{
                $ret['status'] = false;
                $ret['msg'] = $this->getDbError();
            }
        }

         return $ret;
    }

    public function returnData($post){

        $return = array();

        $return['equipment_id']    = !empty($post['equipment_id']) ? intval($post['equipment_id']) : null;
        $return['number']          = !empty($post['number']) ? addslashes($post['number']) : '';
        $return['name']            = !empty($post['name']) ? addslashes($post['name']) : '';
        $return['source']          = !empty($post['source']) ? addslashes($post['source']) : '';
        $return['info']            = !empty($post['info']) ? addslashes($post['info']) : '';
        $return['user_id']         = !empty($post['user_id']) ? intval($post['user_id']) : $_SESSION['USER']['INFO']['user_id'];
        $return['reg_time']        = !empty($post['reg_time']) ? strtotime($post['reg_time']) : time();
        
        $return['del']             = 1;

        return $return;
    }

    public function getEquListByUser($user_id=''){
        
        $fields = "equipment_id,number,name";
    
        $where  =  !empty($user_id) ? " user_id={$user_id} and del=1 " : " del=1 ";
        
        $res  = $this->field($fields)->where($where)->select();
        
        return !empty($res) ? $res : array();
    }

    public function getEquNameAndId($condition=''){

        $fields = "equipment_id,number,name";
        $where  =  !empty($user_id) ? " user_id={$condition} and del=1 " : " del=1 ";
        
        $res  = $this->field($fields)->where($where)->select();
        if(empty($res)){
            return array();
        }

        $return = array();
        foreach ($res as $key => $value) {
            # code...
            $return[$value['equipment_id']] = array('number'=>$value['number'],'name'=>$value['name']);

        }
        return $return;
    }

    public function getEquById($equipment_id){

        $fields = "equipment_id,number,name";
        
        $where  =   " equipment_id={$equipment_id} and del=1" ;
        
        $res  = $this->field($fields)->where($where)->select();
        
        return !empty($res) ? $res[0] : array('equipment_id'=>$equipment_id,'number'=>'','name'=>'');
    }

    public function getEquList(){

        $fields = "equipment_id,number,name";
        
        $where  =   "del=1" ;
        
        $res  = $this->field($fields)->where($where)->select();
        
        return !empty($res) ? $res : array();
    }

}